package scalafx.scene.control

import javafx.scene.control.SelectionMode
import javafx.scene.{ control => jfxsc }
import scalafx.Includes.jfxObjectProperty2sfx
import scalafx.util.SFXDelegate

object MultipleSelectionModel {

  def sfxMultipleSelectionModel2jfx[T](v: MultipleSelectionModel[T]) = v.delegate

}

abstract class MultipleSelectionModel[T](override val delegate: jfxsc.MultipleSelectionModel[T]) extends SelectionModel[T](delegate) with SFXDelegate[jfxsc.MultipleSelectionModel[T]] {

  /**
   * Specifies the selection mode to use in this selection model. The selection mode specifies how many items in the underlying data model can be selected at
   * any one time.
   *
   * By default, the selection mode is SelectionMode.SINGLE.
   */
  def selectionMode = delegate.selectionModeProperty
  def selectionMode_=(v: SelectionMode) {
    selectionMode() = v
  }

  /**
   * Returns a read-only ObservableList of all selected indices. The ObservableList will be updated by the selection model to always reflect changes in
   * selection. This can be observed by adding a ListChangeListener to the returned ObservableList.
   */
  def selectedIndices = delegate.getSelectedIndices

  /**
   * Returns a read-only ObservableList of all selected items. The ObservableList will be updated further by the selection model to always reflect changes in
   * selection. This can be observed by adding a ListChangeListener to the returned ObservableList.
   */
  def selectedItems = delegate.getSelectedItems

  /**
   * This method allows for one or more selections to be set at the same time. It will ignore any value that is not within the valid range (i.e. greater than
   * or equal to zero, and less than the total number of items in the underlying data model). Any duplication of indices will be ignored.
   *
   * If there is already one or more indices selected in this model, calling this method will not clear these selections - to do so it is necessary to first
   * call clearSelection.
   *
   * The last valid value given will become the selected index / selected item.
   *
   *
   */
  // To convert Scala varargs to Java varargs, see http://stackoverflow.com/questions/2334200/transforming-scala-varargs-into-java-object-varargs
  def selectIndices(index: Int, indices: Int*) = delegate.selectIndices(index, indices.map(_.asInstanceOf[Int]): _*)

  /**
   * Selects all indices from the given start index to the item before the given end index. This means that the selection is inclusive of the start index, and
   * exclusive of the end index. This method will work regardless of whether start < end or start > end: the only constant is that the index before the given
   * end index will become the selected index.
   *
   * If there is already one or more indices selected in this model, calling this method will not clear these selections - to do so it is necessary to first 
   * call clearSelection.
   * 
   * @param start The first index to select - this index will be selected.
   * @param end The last index of the selection - this index will not be selected.
   */
  def selectRange(start: Int, end: Int) = delegate.selectRange(start, end)

  /**
   * Convenience method to select all available indices.
   */
  def selectAll = delegate.selectAll

}